﻿// See https://aka.ms/new-console-template for more information

using Utc_时间戳转换.EFCore;

Console.WriteLine("Hello, World!");

//TimeStampConverter.ConvertTimeStampToUTCTimeString(1681787439);
//TimeStampConverter.ConvertTimeStampToUTCTimeString(1682217039785);
//var longTime =TimeStampConverter.ConvertTimeStampToUTCTimeString(1681787439);
//var dataTime=Convert.ToDateTime(TimeStampConverter.ConvertTimeStampToUTCTimeString((long)1681787439));

//var b = TimeStampConverter.ConvertUTTimeStringToTimeStamp(Convert.ToDateTime("2023/4/18 1:51:19"));

//Console.WriteLine($"当前UTC时间{longTime}");
//Console.WriteLine($"当前UTC时间{dataTime}");
//Console.WriteLine($"UTC时间转时间戳{b}");

//Console.WriteLine($"当前UTC时间{DateTime.UtcNow}");
//long a = TimeStampConverter.ConvertUTTimeStringToTimeStamp(DateTime.UtcNow);
//Console.WriteLine($"当前时间戳{a}");
//Console.WriteLine($"当前时间标准时间{TimeStampConverter.TimeStampToDateTime(a.ToString())}");

//var c= TimeStampConverter.ConvertUTTimeStringToTimeStamp(DateTime.UtcNow);
//var d=Convert.ToDateTime(TimeStampConverter.ConvertTimeStampToUTCTimeString((long)a));

//using BooksDbContext ctx = new BooksDbContext();
//var bookList=ctx.Books.ToList();
//foreach (var book1 in bookList)
//{
//    Console.WriteLine($"UTC时间{book1.PubTime}");
//    Console.WriteLine($"UTC时间转时间戳{TimeStampConverter.ConvertUTTimeStringToTimeStamp(book1.PubTime)}");
//    var long1 = TimeStampConverter.ConvertUTTimeStringToTimeStamp(book1.PubTime);
//    Console.WriteLine($"当前时间标准时间{TimeStampConverter.TimeStampToDateTime(long1.ToString())}");
//}

using CarsDbContext ctx1 = new CarsDbContext();

var car1 = await ctx1.FindAsync<Cars>((long)12);
Console.WriteLine("获取时间：" + car1.PubTime.ToLocalTime());

Books book = new Books
{
    Id = 4,
    Price = 1,
    PubTime = DateTime.UtcNow,
    Title = "zwt",
    AuthorName = "aaa"
};
Cars car = new Cars
{
    Id = 12,
    Price = 1,
    PubTime = DateTime.UtcNow,
    Title = "zwt",
    AuthorName = "aaa"
};

//var books = ctx.Add(book);
var cars = ctx1.Add(car);
//ctx.SaveChanges();
ctx1.SaveChanges();

//var car1 = await ctx1.FindAsync<Cars>((long)6);
//Console.WriteLine("获取时间：" + car1.PubTime);

//ctx.Database.GetDbConnection().Insert(new Books
//{
//    Id = 2,
//    Price = 1,
//    PubTime = d,
//    Title = "zwt",
//    AuthorName = "aaa"
//});
//ctx1.Database.GetDbConnection().Insert(new Cars
//{
//    Id = 2,
//    Price = 1,
//    PubTime = DateTime.UtcNow,
//    Title = "zwt",
//    AuthorName = "aaa"
//});

public static class TimeStampConverter
{
    /// <summary>
    /// 时间戳转Utc时间
    /// </summary>
    /// <param name="llTimeStamp"></param>
    /// <returns></returns>
    public static DateTime ConvertTimeStampToUTCTimeString(long llTimeStamp)
    {
        // 判断时间戳是毫秒还是秒
        if (llTimeStamp.ToString().Length == 10)
        {
            llTimeStamp = llTimeStamp * 1000;
        }
        Console.WriteLine(llTimeStamp);
        Console.WriteLine(Convert.ToString(llTimeStamp, 10));
        var localDateTime = TimeStampToDateTime(llTimeStamp.ToString());
        var univDateTime = localDateTime.ToUniversalTime();
        //var a = TimeStampToDateTime(llTimeStamp.ToString()).ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss.fff");
        var a = TimeStampToDateTime(llTimeStamp.ToString()).ToUniversalTime();
        Console.WriteLine("{0} local time is {1} universal time.",
                                localDateTime,
                                    univDateTime);

        Console.WriteLine($"存到数据库中的时间{Convert.ToDateTime(a)}");

        var b = TimeStampConverter.ConvertUTTimeStringToTimeStamp(Convert.ToDateTime(a));
        Console.WriteLine(b);
        return a;
    }

    /// <summary>
    /// 时间戳转换为时间
    /// </summary>
    /// <param name="timeStamp"></param>
    /// <returns></returns>
    public static DateTime TimeStampToDateTime(string timeStamp)
    {
        DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
        long mTime = long.Parse(timeStamp + "0000");
        TimeSpan toNow = new TimeSpan(mTime);
        return startTime.Add(toNow);
    }

    /// <summary>
    /// Utc时间转时间戳
    /// </summary>
    /// <param name="dt"></param>
    /// <returns></returns>
    public static long ConvertUTTimeStringToTimeStamp(DateTime dt)
    {
        //DateTime dt = DateTime.Parse(strUTCDataTime);
        DateTime startTime = new DateTime(1970, 1, 1);
        long timeStamp = (long)(dt - startTime).TotalMilliseconds; // 相差毫秒数
        return timeStamp;
    }

    /// <summary>
    /// 时间戳转为C#格式时间
    /// </summary>
    /// <param name=”timeStamp”></param>
    /// <returns></returns>
    public static DateTime GetTime(string timeStamp)
    {
        DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
        long lTime = long.Parse(timeStamp + "0000000");
        TimeSpan toNow = new TimeSpan(lTime);
        return dtStart.Add(toNow);
    }
}